<template>
  <div class="version">
    <h4>版本列表
      <el-button style="margin-left:15px;" @click="infoFormVisible = true" size="medium" v-waves type="success" class="el-icon-upload2">
      <span style="padding-left:5px;">发布版本</span>
    </el-button>
    </h4>
    <el-table empty-text="暂无任何版本信息" :data="list" v-loading.body="listLoading" border fit style="width: 100%">
      <el-table-column align="center" label="版本号" >
        <template slot-scope="scope">
          <span>{{scope.row.versionNumber}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="时间">
        <template slot-scope="scope">
          <span>{{scope.row.dateTime}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="version code">
        <template slot-scope="scope">
          <span>{{scope.row.versionCode}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="更新内容">
        <template slot-scope="scope">
          <span>{{scope.row.content}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="备注">
        <template slot-scope="scope">
          <span>{{ !!scope.row.remarks ? scope.row.remarks : '-' }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
            <el-button :type="scope.$index !== 0 ? 'primary' : 'success'"
                       size="small"
                       @click="openDialog(scope.row,scope.$index)"
                       :icon="scope.$index !== 0 ? 'el-icon-document' : 'el-icon-edit'">
              {{ scope.$index === 0 ? '修改' : '详情' }}
            </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" prev-text="上一页" next-text="下一页" @current-change="handleCurrentChange" :current-page.sync="listQuery.current"
                     :page-sizes="[10,20,30]" :page-size="listQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog width="700px"
               :visible.sync="infoFormVisible"
               @close="cancledialog('sendForm')"
    >
      <el-form :model="versionData" :rules="rules" ref="sendForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="版本号 "
                          prop="versionNumber"
                          :label-width="formLabelWidth">
              <el-input
                placeholder="请输入版本号"
                :disabled="!currentIndex"
                v-model="versionData.versionNumber">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="version code "
                          prop="versionCode"
                          :label-width="formLabelWidth">
              <el-input
                placeholder="请输入version code"
                :disabled="!currentIndex"
                v-model="versionData.versionCode">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 说明title -->
        <el-row>
          <el-col :span="12" class="pl30"><h4>更新说明</h4></el-col>
          <el-col :span="10" class="pl30"><h4>内部备注</h4></el-col>
        </el-row>
        <!-- textarea说明 -->
        <el-row class="pl15">
          <el-col :span="11">
            <el-form-item prop="content">
              <el-input
                type="textarea"
                :rows="8"
                :disabled="!currentIndex"
                placeholder="请输入更新说明"
                v-model="versionData.content">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item prop="remarks">
              <el-input
                type="textarea"
                :rows="8"
                :disabled="!currentIndex"
                placeholder="请输入备注"
                v-model="versionData.remarks">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-show="currentIndex" type="primary" size="mini" @click="sendEvent">保存</el-button>
        <el-button @click="infoFormVisible = false" size="mini">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import waves from '@/directive/waves' // 水波纹指令
  import {
    getVersionList,
    addUpdateVs
  } from '@/api/version'

  export default {
    name: 'version',
    directives: {
      waves
    },
    data() {
      const CheckVersionNum = (rule, value, callback) => {
        let tempValue = value * 1;
        if (!value) {
          callback(new Error('请输入version Code'));
        } else if (!Number.isInteger(tempValue)) {
          callback(new Error('version Code必须是数字'));
        }else{
          callback();
        }
      }
      return {
        currentIndex : true, //默认为true,只有在选中非第一个版本时才为false
        infoFormVisible:false,
        formLabelWidth:"100px",
        list: [],
        total:null,
        listLoading: true,
        listQuery: {
          current: 1,
          pagesize: 10
        },
        versionData:{},
        rules: {
          versionNumber: [
            {required: true, message: '请输入版本号', trigger: 'blur'},
          ],
          versionCode:[
            {validator: CheckVersionNum, trigger: 'blur'},
          ],
          content:[
            {required: true, message: '请输入更新说明', trigger: 'blur'}
          ]
        },
      }
    },
    created() {
      this.getList();
    },
    methods: {
      sendEvent(){
        this.$refs.sendForm.validate(valid=> {
          if (!valid) {
            return
          }
          this.versionData['versionCode'] =  this.versionData['versionCode'] * 1;
          addUpdateVs(this.versionData).then(res=>{
              this.$message.success(res.msg);
              this.infoFormVisible = false;
              this.getList();
          }).catch(res=>{
              this.$message.error(res.msg);
          })
        })
      },
      getList(){
        this.listLoading = true;
        getVersionList(this.listQuery).then(response=>{
            let temp = response.data;
            this.list = temp.info;
            this.total = temp.total
            this.listLoading = false;
        }).catch(res => {
          this.$message.error(res);
          this.listLoading = false;
        })
      },
      handleSizeChange(val) {
        this.listQuery.pagesize = val
        this.getList()
      },
      handleCurrentChange(val){
        this.listQuery.current = val
        this.getList()
      },
      cancledialog(userForm){
        //重置表单
        this.$refs[userForm].resetFields();
        this.versionData = {}; //重置数据
        this.currentIndex = true; //重置
      },
      openDialog(tempData,index){
        let data = Object.assign({},tempData);
        this.infoFormVisible = true;
        index !== 0 ? this.currentIndex = false : null//赋值当前索引
        tempData ? this.versionData = data : null //赋值数据
      }
    }
  }
</script>

<style type="text/scss" lang="scss" scoped>
  .version {
    .pl30 {
      padding-left:30px;
    }
    .pl15 {
      padding-left:15px;
    }
    .el-input{
      width: 200px;
    }
    h4 {
      margin:10px 0px;
    }
  }
</style>
